package gxnualbert.LambdaDemo.test5;

/**
 * @auther gxnualbert
 * @create 2021-05-30-15:10
 */

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 参考 csdn 上面的博客：https://blog.csdn.net/bibiboyx/article/details/84257741
 * https://www.liaoxuefeng.com/article/974382480887712
 */
public class CsdnLambda {


    @Test
    public void test1() {
        List<User> userList = Arrays.asList(
                new User("张三", 10),
                new User("李四", 4),
                new User("王五", 9),
                new User("赵六", 15),
                new User("田七", 17),
                new User("郭八", 6),
                new User("毛九", 14),
                new User("付十", 20)
        );
        Collections.sort(userList, (u1, u2) -> u1.getAge() - u2.getAge());
        userList.sort(Comparator.comparingInt(User::getAge));
        System.out.println(userList);
        userList.stream().forEach(user -> {
            System.out.print(user.getAge());
            System.out.print(user.getName());
            System.out.println(" ");
        });
    }


    @Test
    public void ListTest(){
        List<String> strings = Arrays.asList(
                "A",
                "BC",
                "C",
                "BG",
                "FR",
                "OK",
                "l",
                "W",
                "M"
        );

        //Java8之前操作List
        for (String str : strings) {
            System.out.println(str);
        }

        //Java8 lambda遍历list
        strings.forEach(str -> System.out.println(str));
        strings.forEach(System.out::println);

        //
        System.out.println("++++++++++++++++++++++++++++++++++");
        strings.stream().filter(s -> s.contains("B") || s.contains("C")).forEach(System.out::println);
    }
}
